/****************************************************************************
 *
 * Copyright (C) 2003-2008 Los Alamos National Security, LLC
 *                         Packet Analytics Corporation
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License Version 2 as
 * published by the Free Software Foundation.  You may not use, modify or
 * distribute this program under any other version of the GNU General
 * Public License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 *
 ****************************************************************************/
package nfse.dataset.proofpoint;

import java.sql.Timestamp;

import nfse.NetFSE;
import nfse.NetFSERecord;
import nfse.NetFSEUtil;
import nfse.gui.record.FlexRecord;
import nfse.gui.record.FlexRecordField;

/*******************************************************************************
 * This class is used to parse and store TippingPoint records.
 * 
 * @author uphoff
 */
public class ProofPointRecord extends NetFSERecord {

    /***************************************************************************
     * Defines what version of the log format was used to generate this object
     */
    public int type = -1;

    /***************************************************************************
     * 
     */
    public String sessionID = "";

    /***************************************************************************
     * 
     */
    public String toEmail = "";

    /***************************************************************************
     * 
     */
    public String fromEmail = "";

    /***************************************************************************
     * 
     */
    public String virusName = "";

    public String subject = "";

    public static void main(String[] args) {
        ProofPointRecord rec = new ProofPointRecord();
        rec.parse(args, rec);
    }
    
    public String get(int field) {
        String temp = "";
        int len = 0;
        switch (field) {
        case (NetFSE.FIELD_START_TS):
            temp = Long.toHexString(this.getStartTS().getTime() / 1000);
            len = 8;
            break;
        case (NetFSE.FIELD_SRC_IP):
            temp = Long.toHexString(this.getSrcIP());
            len = 8;
            break;
        default:
            temp = null;
            break;
        }
        if (temp != null) {
            while (temp.length() < len)
                temp = "0" + temp;
        }
        return temp;
    }

    public FlexRecord getFlexRecord() {
        FlexRecord temp = new FlexRecord();
        temp.values = new String[7];
        int idx = -1;
        temp.values[++idx] = this.getStartTS().toString();
        temp.values[++idx] = this.getSrcAddr();
        temp.values[++idx] = this.toEmail;
        temp.values[++idx] = this.fromEmail;
        temp.values[++idx] = this.sessionID + "";
        temp.values[++idx] = this.virusName;
        temp.values[++idx] = this.subject;
        return temp;
    }

    public FlexRecordField[] getFlexRecordFields() {
        FlexRecordField[] temp = new FlexRecordField[7];
        int idx = -1;
        temp[++idx] = new FlexRecordField("Start Time", "startts", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_TIMESTAMP);
        temp[++idx] = new FlexRecordField("Src IP", "srcip", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_IP_ADDR);
        temp[++idx] = new FlexRecordField("To Email", "toemail", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_EMAIL_ADDR);
        temp[++idx] = new FlexRecordField("From Email", "fromemail", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_EMAIL_ADDR);
        temp[++idx] = new FlexRecordField("Session ID", "sessionid", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_NONE);
        temp[++idx] = new FlexRecordField("Virus Name", "virus", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_NONE);
        temp[++idx] = new FlexRecordField("Subject", "subject", FlexRecordField.TYPE_STRING,
                FlexRecordField.SUB_TYPE_NONE);
        return temp;
    }

   

    /***************************************************************************
     * Parses a raw ProofPoint record.
     * 
     * @param line
     *            The log to parse into an object
     * @return The data structure for this log record or null on error
     */
    public NetFSERecord generate(String line, int sensor) {
        try {
            ProofPointRecord pp = new ProofPointRecord();
            pp.setTypeID(typeID);
            pp.setTypeName(typeName);
            String[] tokens = line.split(",");
            if (tokens.length < 6)
                throw (new Exception("Incorrect number of ProofPoint tokens (" + tokens.length + ") for '" + line
                        + "'."));
            pp.sessionID = tokens[0];
            pp.setStartTS(Timestamp.valueOf(tokens[1]));
            //pp.setSrcAddr(tokens[2]);
            pp.setSrcIP(Long.parseLong(NetFSEUtil.convertIPtoHex(tokens[2]), 16));
            pp.fromEmail = tokens[3];
            pp.toEmail = tokens[4];
            pp.virusName = tokens[5];
            if (tokens.length > 6)
                pp.subject = tokens[6];
            pp.setInitialString(line);
            return pp;
        } catch (Exception E) {
            E.printStackTrace();
        }
        return null;
    }

    public String getCreateFieldTableSQL(String table) {
        StringBuffer temp = new StringBuffer("create table " + table + " (ID bigint, ");
        temp.append("sessionid varchar(12),");
        temp.append("srcip inet,");
        temp.append("fromemail varchar(255),");
        temp.append("toemail varchar(255),");
        temp.append("virus varchar(255),");
        temp.append("subject varchar(255),");
        temp.append("startts timestamp");
        temp.append(")");
        return temp.toString();
    }
    
    public String getCreateFieldTableSQL_MySQL(String table) {
        StringBuffer temp = new StringBuffer("create table " + table + " (ID bigint, ");
        temp.append("sessionid varchar(12),");
        temp.append("srcip bigint,");
        temp.append("fromemail varchar(255),");
        temp.append("toemail varchar(255),");
        temp.append("virus varchar(255),");
        temp.append("subject varchar(255),");
        temp.append("startts datetime");
        temp.append(")");
        return temp.toString();
    }

    

    public String getInsertFieldTableSQL(int id, String table) {
        StringBuffer temp = new StringBuffer("insert into " + table + " values (" + id + ",'");
        temp.append(sessionID + "','");
        temp.append(NetFSEUtil.convertLongToIP(getSrcIP()) + "','");
        temp.append(fromEmail + "','");
        temp.append(toEmail + "','");
        temp.append(virusName + "','");
        temp.append(subject + "',");
        temp.append("'" + getStartTS() + "'");
        temp.append(")");
        return temp.toString();
    }
    
    public String getInsertFieldTableSQL_MySQL(int id, String table) {
        StringBuffer temp = new StringBuffer("insert into " + table + " values (" + id + ",'");
        temp.append(sessionID + "',");
        temp.append(getSrcIP() + ",'");
        temp.append(fromEmail + "','");
        temp.append(toEmail + "','");
        temp.append(virusName + "','");
        temp.append(subject + "',");
        temp.append("'" + getStartTS() + "'");
        temp.append(")");
        return temp.toString();
    }

    public String createMergeTableSQL(String table, String[] tableNames) {
        StringBuffer temp = new StringBuffer("create table ");
        temp.append(table);
        temp.append("(");
        temp.append("srcip varchar(12)");
        temp.append(",");
        temp.append("startts datetime");
        temp.append(",");
        temp.append("record blob");
        temp.append(",");
        temp.append("record_hex blob");
        temp.append(") ENGINE=MERGE UNION (");
        for (int i = 0; i < tableNames.length; i++) {
            if (i > 0)
                temp.append(",");
            temp.append(tableNames[i]);
        }
        temp.append(") INSERT_METHOD=LAST");
        // System.out.println(temp.toString());
        return temp.toString();
    }

}